<?php

include_once("class_lib.php");

$ctrl = new controller();

$tag = stripslashes($_GET["tag"]);
$taggable = $_GET["taggable"];

$itemsInTag = $ctrl->getItemsInTag($tag);

if (!isset($taggable))
{
	$taggable = "None";
}

if ($taggable == "All")
{
	$taggableItems = $ctrl->getItems();
	$taggableItems = $ctrl->getItemsOutsideTag($tag, $taggableItems);
}
elseif ($taggable == "None")
{
	$taggableItems = array();
}
elseif ($taggable == "Untagged")
{
	$taggableItems = $ctrl->getUntaggedItems();
}
else
{
	$taggableItems = $ctrl->getItemsInTag($taggable);
	$taggableItems = $ctrl->getItemsOutsideTag($tag, $taggableItems);
}

if (is_array($itemsInTag) && count($itemsInTag) > 0)
{
	?>

	<p>Items tagged "<?= $tag; ?>"</p>

	<?php

	foreach($itemsInTag as $item)
	{
		print "<li>" . $item->getName() . " <a class=\"untag\" href=\"/untag/" . $item->getName() . "\">(untag)</a></li>";
	}	
}
elseif (isset($_GET["tag"]))
{
	?>

	<p>No items tagged "<?= $tag; ?>"</p>

	<?php
}

?>

<p>
	Taggable items:
<?php

if ($taggable != "All")
{
	print " | <a href=\"?tag=" . $tag . "&taggable=All\">All</a>";
}
else
{
	print " | All";
}

if (isset($taggable) && $taggable != "None")
{
	print " | <a href=\"?tag=" . $tag . "&taggable=None\">None</a>";
}
else
{
	print " | None";
}

if ($taggable != "Untagged")
{
	print " | <a href=\"?tag=" . $tag . "&taggable=Untagged\">Untagged</a>";
}
else
{
	print " | Untagged";
}

foreach ($ctrl->getTags() as $t)
{
	if ($t->getName() != $tag)
	{
		if ($t->getName() != $taggable)
		{
			print " | <a href=\"?tag=" . $tag . "&taggable=" . $t->getName() . "\">" . $t->getName() . "</a>";
		}
		else
		{
			print " | " . $t->getName();
		}
	}
}

?>

</p>

<?php

if (is_array($taggableItems) && count($taggableItems) > 0)
{
	foreach($taggableItems as $item)
	{
		if ($_GET["item"] == $item->getName())
		{
			print "<li>" . $item->getName() . "</li>";
		}
		else
		{
			print "<li>" . $item->getName() . " <a class=\"tag\" href=\"/tag/" . $item->getName() . "\">(tag)</a></li>";
		}

	}	
}

?>
